package com.fc.v2.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.fc.v2.mapper.auto.ApplyProjectMapper;
import com.fc.v2.mapper.auto.ExperprojectMapper;
import com.fc.v2.mapper.auto.FinishProjectMapper;
import com.fc.v2.model.Examine;
import com.fc.v2.model.ProjectState;
import com.fc.v2.model.auto.*;
import com.fc.v2.shiro.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.v2.common.base.BaseService;
import com.fc.v2.common.support.ConvertUtil;
import com.fc.v2.mapper.auto.ScoreProjectMapper;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.util.SnowflakeIdWorker;
import com.fc.v2.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 专家鉴定评分 ScoreProjectService
 * @Title: ScoreProjectService.java 
 * @Package com.fc.v2.service 
 * @author Archie_自动生成
 * @email ${email}
 * @date 2021-11-15 10:58:33  
 **/
@Service
public class ScoreProjectService implements BaseService<ScoreProject, ScoreProjectExample>{
	@Autowired
	private ScoreProjectMapper scoreProjectMapper;

	@Autowired
	private ApplyProjectMapper applyProjectMapper;

	@Autowired
	private FinishProjectMapper finishProjectMapper;

	@Autowired
	private ExperprojectMapper experprojectMapper;
	
      	   	      	      	      	      	      	      	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<ScoreProject> list(Tablepar tablepar,ScoreProject scoreProject,String proId){
	        ScoreProjectExample testExample=new ScoreProjectExample();
			//搜索
			if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
	        	testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
	        }else {//大搜索
				//选取该项目的所有鉴定评分专家
	        	testExample.createCriteria().andProjectIdEqualTo(proId);
	        }
//			表格排序
//			if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
//	        	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
//	        }else{
//	        	testExample.setOrderByClause("id ASC");
//	        }
	        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
	        List<ScoreProject> list= scoreProjectMapper.selectByExample(testExample);
	        PageInfo<ScoreProject> pageInfo = new PageInfo<ScoreProject>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=ConvertUtil.toListStrArray(ids);
			ScoreProjectExample example=new ScoreProjectExample();
			example.createCriteria().andIdIn(lista);
			return scoreProjectMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public ScoreProject selectByPrimaryKey(String id) {
				
			return scoreProjectMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(ScoreProject record) {
		return scoreProjectMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(ScoreProject record) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		//将当前专家用户的id添加到专家id中
		record.setExpertId(ShiroUtils.getUserId());
		//在项目中添加专家审核完成状态
//		String projectId = record.getProjectId();
//		ApplyProject applyProject = applyProjectMapper.selectByPrimaryKey(projectId);
//		applyProject.setProState(Examine.EXPECT_DONE.code);
//		applyProjectMapper.updateByPrimaryKeySelective(applyProject);
		return scoreProjectMapper.insertSelective(record);
	}

	/**
	 * 添加
	 */
	public int insertSelectiveFinish(ScoreProject record,String projectId) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		//将当前专家用户的id添加到专家id中
		record.setExpertId(ShiroUtils.getUserId());
		//在项目专家映射表中，审核进度为专家已完成（4）
		ExperprojectExample example1 = new ExperprojectExample();
		List<Experproject> experprojectList = new ArrayList<>();
		example1.createCriteria().andExpertIdEqualTo(ShiroUtils.getUserId()).andOther1EqualTo("finish").andProIdEqualTo(projectId);
		experprojectList = experprojectMapper.selectByExample(example1);
		Experproject experproject = experprojectList.get(0);
		experproject.setCheckProcess(Examine.EXPECT_DONE.code);
		//添加专家操作时间
		experproject.setManUpdateTime(new Date());
		experprojectMapper.updateByPrimaryKeySelective(experproject);
		//判断所有专家的完成，若都完成则，项目的状态为专家鉴定完成
		ExperprojectExample example2 = new ExperprojectExample();
		example2.createCriteria().andProIdEqualTo(projectId);
		List<Experproject> epList = new ArrayList<>();
		epList = experprojectMapper.selectByExample(example2);
		int n = 0;
		for (Experproject ep:epList) {
			if(ep.getCheckProcess() == Examine.EXPECT_DONE.code){
				n++;
			}
		}
		if(n == epList.size()){
			FinishProject finishProject = new FinishProject();
			finishProject.setId(projectId);
			finishProject.setSklPass(Examine.EXPECT_DONE.code);
			//专家审核状态为已完成
			finishProject.setExpertPass(Examine.PASSED.code);
			finishProjectMapper.updateByPrimaryKeySelective(finishProject);
		}

		return scoreProjectMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(ScoreProject record, ScoreProjectExample example) {
		
		return scoreProjectMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(ScoreProject record, ScoreProjectExample example) {
		
		return scoreProjectMapper.updateByExample(record, example);
	}

	@Override
	public List<ScoreProject> selectByExample(ScoreProjectExample example) {
		
		return scoreProjectMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(ScoreProjectExample example) {
		
		return scoreProjectMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(ScoreProjectExample example) {
		
		return scoreProjectMapper.deleteByExample(example);
	}
	
	/**
	 * 修改权限状态展示或者不展示
	 * @param scoreProject
	 * @return
	 */
	public int updateVisible(ScoreProject scoreProject) {
		return scoreProjectMapper.updateByPrimaryKeySelective(scoreProject);
	}


}
